package cn.jyc.ssm03.service;

import cn.jyc.ssm03.dao.CustomerMapper;
import cn.jyc.ssm03.pojo.Customer;
import cn.jyc.ssm03.utils.JsonUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustService {
    @Autowired
    private CustomerMapper cm;

    public String getCust(Integer p1, Integer p2, Customer cus) {
        Page<Object> page = PageHelper.startPage(p1, p2);
        List<Customer> cuss = cm.getCus(cus);
        Map<String, Object> m = new HashMap<>();
        System.out.println("qweqwe："+cuss.toString());
        m.put("rows", cuss);
        m.put("total", page.getTotal());
        return JsonUtil.objectToJson(m);
    }

    public String add(Customer cus) {
        Map<String, Object> res = new HashMap<>();
        try {
            cm.add(cus);
        } catch (Exception e) {
            res.put("errorMsg", e.getMessage());
        }
        return JsonUtil.objectToJson(res);
    }

    public String delCust(String id) {
        Map<String, Object> res = new HashMap<>();
        try {
            cm.del(id);
            res.put("success", true);
        } catch (Exception e) {
            res.put("errorMsg", e.getMessage());
        }
        return JsonUtil.objectToJson(res);
    }

    public String updCust(Customer cus){
        Map<String, Object> res = new HashMap<>();
        try{
        cm.updCust(cus);
        } catch (Exception e){
            res.put("errorMsg",e.getMessage());
        }
        return JsonUtil.objectToJson(res);
    }

    public XSSFWorkbook exportCusExcel(){
//        创建工作簿对象
        XSSFWorkbook sheets = new XSSFWorkbook();
//        创建sheet页对象
        XSSFSheet sheet = sheets.createSheet("客户信息");
//        创建第一行的行对象
        XSSFRow row = sheet.createRow(0);
//        创建第一行的第一个单元格对象
        XSSFCell cell = row.createCell(0);
//        创建并添加合并单元格的对象
        CellRangeAddress cellAddresses = new CellRangeAddress(0, 0, 0, 5);
        sheet.addMergedRegion(cellAddresses);
//        创建并设置单元格样式
        XSSFCellStyle cellStyle = sheets.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cell.setCellStyle(cellStyle);
//        添加数据
        cell.setCellValue("客户信息列表");
//        创建表头行
        XSSFRow row1 = sheet.createRow(1);
//        创建表头列并添加数据
        row1.createCell(0).setCellValue("客户编号");
        row1.createCell(1).setCellValue("客户姓名");
        row1.createCell(2).setCellValue("客户性别");
        row1.createCell(3).setCellValue("客户地址");
        row1.createCell(4).setCellValue("客户电话");
        row1.createCell(5).setCellValue("客户职业");
//        获取客户列表并赋值给单元格
        int index=2;
        List<Customer> cus = cm.getCus(null);
        for (Customer c : cus) {
            XSSFRow rowx = sheet.createRow(index);
            rowx.createCell(0).setCellValue(c.getIdentity());
            rowx.createCell(1).setCellValue(c.getCustname());
            rowx.createCell(2).setCellValue(c.getSex()==0?"女":"男");
            rowx.createCell(3).setCellValue(c.getAddress());
            rowx.createCell(4).setCellValue(c.getPhone());
            rowx.createCell(5).setCellValue(c.getCareer());
            index++;
        }
        return sheets;
    }

    public Map<String,Object> importCusExcel(XSSFWorkbook workbook){
        Map<String,Object> res = new HashMap<>();
        try {
            XSSFSheet sheet = workbook.getSheet("客户信息");
            int firstRowNum = sheet.getFirstRowNum();
            int lastRowNum = sheet.getLastRowNum();
            for (int i = firstRowNum+2; i < lastRowNum; i++) {
                XSSFRow row = sheet.getRow(i);
                String q1 = row.getCell(0).getStringCellValue();
                String q2 = row.getCell(1).getStringCellValue();
                String q3 = row.getCell(2).getStringCellValue();
                String q4 = row.getCell(3).getStringCellValue();
                String q5 = row.getCell(4).getStringCellValue();
                String q6 = row.getCell(5).getStringCellValue();
                Customer cu = new Customer(q1, q2, q3.equals("男") ? 1 : 0, q4, q5, q6);
                cm.add(cu);
            }
            res.put("success",200);
        } catch (Exception e){
            e.printStackTrace();
            res.put("errorMsg",e.getMessage());
        }
        return res;
    }
}
